package team1.project2.vmlogs;

import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.List;

/**
 * 
 * @author Deepali Pawar
 * 
 */
public class LogDataLoader {
	
	public int loadKernelWarnings(List<KernelMessage> kernelList) {
		
		Connection conn = null;
		Statement stmt = null;
		int[] updateCounts = null;
		ResultSet rs = null;
		try {
			long startTime = System.currentTimeMillis();
			String sql = "insert into kernel_warnings (date_time, "
			        + " UPTIME_OF_HOST,CPU_OR_CORE_NAME,WID,CODE_REPORTING_MESSAGE,LINE_OF_REPORTING_MESSAGE, "
			        + " MESSAGE_CONTENT) values(?,?,?,?,?,?,?)";
			System.out.println("sql=" + sql);
			conn = NormalisedConnection.getConnection();
			PreparedStatement prest = conn.prepareStatement(sql);
			
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
			        ResultSet.CONCUR_UPDATABLE);
			conn.setAutoCommit(false);
			for (KernelMessage msg : kernelList) {
				prest.setTimestamp(1,
				        new Timestamp(msg.getDateTime().getTime()));
				prest.setString(2, msg.getUptimeOfHost());
				prest.setString(3, msg.getCpuOrCoreName());
				prest.setString(4, msg.getWid());
				prest.setString(5, msg.getCodeReportingMessage());
				prest.setString(6, msg.getLineOfReportingMessage());
				prest.setString(7, msg.getMessageContent());
				prest.addBatch();
			}
			
			updateCounts = prest.executeBatch();
			conn.commit();
			long endTime = System.currentTimeMillis();
			System.out
			        .println("Time to load data through JDBC in normalised DB 'stations_info' Table:"
			                + (endTime - startTime) + "msec");
			
		} catch (BatchUpdateException b) {
			System.err.println("SQLException: " + b.getMessage());
			System.err.println("SQLState: " + b.getSQLState());
			System.err.println("Message: " + b.getMessage());
			System.err.println("Vendor error code: " + b.getErrorCode());
			System.err.print("Update counts: ");
			updateCounts = b.getUpdateCounts();
			for (int i = 0; i < updateCounts.length; i++) {
				System.err.print(updateCounts[i] + " ");
			}
		} catch (SQLException ex) {
			System.err.println("SQLException: " + ex.getMessage());
			System.err.println("SQLState: " + ex.getSQLState());
			System.err.println("Message: " + ex.getMessage());
			System.err.println("Vendor error code: " + ex.getErrorCode());
		} catch (Exception e) {
			System.err.println("Exception: " + e.getMessage());
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (Exception ignore) {
			}
		}
		return updateCounts.length;
	}
	
	public int loadKernelBatch(List<KernelMessage> kernelList) {
		
		Connection conn = null;
		Statement stmt = null;
		int[] updateCounts = null;
		ResultSet rs = null;
		try {
			long startTime = System.currentTimeMillis();
			String sql = "insert into kernel_logs (date_time, "
			        + " UPTIME_OF_HOST,CPU_OR_CORE_NAME,WID,CODE_REPORTING_MESSAGE,LINE_OF_REPORTING_MESSAGE, "
			        + " MESSAGE_CONTENT) values(?,?,?,?,?,?,?)";
			System.out.println("sql=" + sql);
			conn = NormalisedConnection.getConnection();
			PreparedStatement prest = conn.prepareStatement(sql);
			
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
			        ResultSet.CONCUR_UPDATABLE);
			conn.setAutoCommit(false);
			for (KernelMessage msg : kernelList) {
				prest.setTimestamp(1,
				        new Timestamp(msg.getDateTime().getTime()));
				prest.setString(2, msg.getUptimeOfHost());
				prest.setString(3, msg.getCpuOrCoreName());
				prest.setString(4, msg.getWid());
				prest.setString(5, msg.getCodeReportingMessage());
				prest.setString(6, msg.getLineOfReportingMessage());
				prest.setString(7, msg.getMessageContent());
				prest.addBatch();
			}
			
			updateCounts = prest.executeBatch();
			conn.commit();
			long endTime = System.currentTimeMillis();
			System.out
			        .println("Time to load data through JDBC in normalised DB 'stations_info' Table:"
			                + (endTime - startTime) + "msec");
			
		} catch (BatchUpdateException b) {
			System.err.println("SQLException: " + b.getMessage());
			System.err.println("SQLState: " + b.getSQLState());
			System.err.println("Message: " + b.getMessage());
			System.err.println("Vendor error code: " + b.getErrorCode());
			System.err.print("Update counts: ");
			updateCounts = b.getUpdateCounts();
			for (int i = 0; i < updateCounts.length; i++) {
				System.err.print(updateCounts[i] + " ");
			}
		} catch (SQLException ex) {
			System.err.println("SQLException: " + ex.getMessage());
			System.err.println("SQLState: " + ex.getSQLState());
			System.err.println("Message: " + ex.getMessage());
			System.err.println("Vendor error code: " + ex.getErrorCode());
		} catch (Exception e) {
			System.err.println("Exception: " + e.getMessage());
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (Exception ignore) {
			}
		}
		return updateCounts.length;
	}
	
	public int loadSysBatch(List<SysMessage> sysLogList) {
		
		Connection conn = null;
		Statement stmt = null;
		int[] updateCounts = null;
		ResultSet rs = null;
		try {
			long startTime = System.currentTimeMillis();
			String sql = "insert into sys_logs (date_time, "
			        + " "
			        + " event) values(?,?)";
			System.out.println("sql=" + sql);
			conn = NormalisedConnection.getConnection();
			PreparedStatement prest = conn.prepareStatement(sql);
			
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
			        ResultSet.CONCUR_UPDATABLE);
			conn.setAutoCommit(false);
			for (SysMessage msg : sysLogList) {
				prest.setTimestamp(1,
				        new Timestamp(msg.getDateTime().getTime()));
				prest.setString(2, msg.getEvent());
				prest.addBatch();
			}
			
			updateCounts = prest.executeBatch();
			conn.commit();
			long endTime = System.currentTimeMillis();
			System.out
			        .println("Time to load data through JDBC in normalised DB 'stations_info' Table:"
			                + (endTime - startTime) + "msec");
			
		} catch (BatchUpdateException b) {
			System.err.println("SQLException: " + b.getMessage());
			System.err.println("SQLState: " + b.getSQLState());
			System.err.println("Message: " + b.getMessage());
			System.err.println("Vendor error code: " + b.getErrorCode());
			System.err.print("Update counts: ");
			updateCounts = b.getUpdateCounts();
			for (int i = 0; i < updateCounts.length; i++) {
				System.err.print(updateCounts[i] + " ");
			}
		} catch (SQLException ex) {
			System.err.println("SQLException: " + ex.getMessage());
			System.err.println("SQLState: " + ex.getSQLState());
			System.err.println("Message: " + ex.getMessage());
			System.err.println("Vendor error code: " + ex.getErrorCode());
		} catch (Exception e) {
			System.err.println("Exception: " + e.getMessage());
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (Exception ignore) {
			}
		}
		return updateCounts.length;
	}
	
	private String escapeSingleQuote(String s) {
		return s.replaceAll("'", "''");
		
	}
	
}